package com.xielx.ris.statistic;

import com.xielx.ris.statistic.bean.ReportRow;
import com.xielx.ris.statistic.excel.ExcelHandler;
import org.joda.time.DateTime;
import org.springframework.util.CollectionUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class MainClass {

    public static void main(String[] args) throws Exception {
        Path path = getExcelPath();
        String tjrq = getTjrq();
        ReportRow.setTjrq(tjrq);
        ReportRow.setGxsj(new Date());
        ExcelHandler handler = ExcelHandler.getInstance(path.toFile());
        List<ReportRow> list = handler.readExcel();
        File sqlDir = new File("sql");
        if (!sqlDir.exists()) {
            sqlDir.mkdir();
        }
        File result = new File("sql\\"+tjrq+".sql");
        if (result.exists()) {
            System.out.print("生成的SQL已经存在，是否覆盖[y/n]：");
            if ("n".equalsIgnoreCase(new Scanner(System.in).next())) {
                return;
            }else{
                result.delete();
            }
        }
        try(BufferedWriter fw = new BufferedWriter(new FileWriter(result))){
            for (int i = 0; i < list.size(); i++) {
                if ((i+1)%200==0) {
                    fw.write("commit;");
                    fw.newLine();
                }
                fw.write(list.get(i).toString());
                fw.newLine();
            }
            fw.write("commit;");
            fw.flush();
        }
//        DbUtil.insert(list);
        System.out.print("执行完成！");
    }

    private static String getTjrq() {
        String tjrq = new DateTime().plusMonths(-1).toString("yyyyMM");
        System.out.print("统计周期是否为【" + tjrq + "】，请输入[y/n]：");
        Scanner input = new Scanner(System.in);
        String tt = input.next();
        if ("Y".equalsIgnoreCase(tt)) {
            return tjrq;
        }
        DateFormat sdf = new SimpleDateFormat("yyyyMM");
        do {
            try{
                System.out.print("请输入格式为【yyyyMM】的统计周期：");
                input = new Scanner(System.in);
                tt = input.next();
                sdf.parse(tt);
                return tt;
            }catch (Exception e){
                System.out.println("ERROR：输入的格式有问题，请重新输入！");
            }
        } while (true);
    }

    private static Path getExcelPath() throws IOException {
        List<Path> pathList = Files.list(Paths.get(".")).filter(temp -> temp.getFileName().toString().toUpperCase().endsWith(".XLS")).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(pathList)) {
            System.out.println("请在ris-statistic.jar的同级目录下旋转通报的excle文件");
            System.exit(-1);
            return null;
        }
        System.out.println("\t序号\t|\t\t\t\t目录");
        for (int i = 0; i < pathList.size(); i++) {
            System.out.println(String.format("\t %s\t|\t%s", i + 1, pathList.get(i).getFileName().toString()));
        }
        int index = -1;
        do {
            System.out.print("请选择需要生成的excel的序号：");
            try {
                Scanner input = new Scanner(System.in);
                index = input.nextInt();
            } catch (Exception e) {
                System.out.println("ERROR:请输入数字!");
                continue;
            }
            if (index > 0 && index < pathList.size()+1) {
                break;
            }
            System.out.println("ERROR:序号的范围超过了有效范围！");
        } while (true);
        Path path = pathList.get(index - 1);
        return path;
    }

}
